﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zoo.Cat.Domain.Entities;
using Zoo.Cat.Domain.Entities.Jobseekers;
using Zoo.Infrastructure.Data.Core;
using Zoo.Infrastructure.Caching;
using Zoo.Infrastructure.Events;

namespace Zoo.Cat.Application.Jobseekers {
    public class JobseekerService : IJobseekerService {
        #region Constants

        #endregion
        #region Fields
        private readonly IRepository<Jobseeker> _jobseekerRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        #endregion

        #region Ctor
        public JobseekerService( IRepository<Jobseeker>  jobseekerRepository,
            ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            this._cacheManager = cacheManager;
            this._eventPublisher = eventPublisher;
            this._jobseekerRepository = jobseekerRepository;
        }
        #endregion


        public Jobseeker GetJobseekerByEmail(string email) {
            //return new Jobseeker {
            //    JobseekerId = Guid.NewGuid(),
            //    Email = "jobseeker-backend@myhome.com",
            //    Active = true,
            //    Username = "yidiao",
            //};

            if (string.IsNullOrWhiteSpace(email)) {
                return null;
            }

            var query = from u in _jobseekerRepository.Table
                        orderby u.Id
                        where u.Email == email
                        select u;
            var user = query.FirstOrDefault();
            return user;
        }

        public virtual void UpdateJobseeker(Jobseeker user) {
            if (user == null)
                throw new ArgumentNullException("Jobseeker");

            _jobseekerRepository.Update(user);
            _eventPublisher.EntityUpdated(user);
              
        }

 }
}
